﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace SuperKit {
    /// <summary>
    /// CombineWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CombineWindow : Window {
        private const char NONE_CHAR = '\n';
        public List<string> CombineFiles { get; set; }
        public List<string> CombineNames { get; set; }

        public CombineWindow() {
            InitializeComponent();
            CombineFiles = new List<string>();
            CombineNames = new List<string>();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {

            list_Files.Children.Clear();
            foreach (var i in CombineFiles) {
                FileInfo info = new FileInfo(i);
                TextBox box = new TextBox();
                box.Margin = new Thickness(3);
                box.Text = info.Name;
                list_Files.Children.Add(box);
            }

            txtCombineDirectory.Text = GetCombineFileName();
        }

        private string GetCombineFileName() {
            int index = GetCombineFileNameIndex();
            var first = CombineNames[0];
            int sub = Math.Min(index, first.Length);
            return first.Substring(0, sub);
        }

        private int GetCombineFileNameIndex() {
            int maxPoint = 0;
            for (int i = 0; i < 100; i++) {
                var first = SafeGet(CombineNames[0], i);
                if (first == NONE_CHAR) {
                    return maxPoint;
                }                
                foreach (var s in CombineNames) {
                    if (SafeGet(s, i) != first) {
                        return maxPoint;
                    }
                }
                maxPoint++;
            }
            return maxPoint;
        }

        private char SafeGet(string str, int index) {
            if (index < 0 || index >= str.Length) {
                return NONE_CHAR;
            }
            return str[index];
        }

        public void Add(string fullName) {
            CombineFiles.Add(fullName);
            CombineNames.Add(Path.GetFileName(fullName));
        }

        private void AutoNumber_Click(object sender, RoutedEventArgs e) {
            for (int i = 0; i < CombineNames.Count; i++) {
                var box = list_Files.Children[i] as TextBox;
                box.Text = $"{i + 1}_{CombineNames[i]}";
            }
        }

        private void Combine_Click(object sender, RoutedEventArgs e) {
            // 建立新的目录
            var first = new FileInfo(CombineFiles[0]);
            var newDir = Path.Combine(first.DirectoryName, txtCombineDirectory.Text.Trim());
            if (!Directory.Exists(newDir)) {
                Directory.CreateDirectory(newDir);
            }
            for (int i = 0; i < CombineFiles.Count; i++) {
                // 通过txt的项目重新组建名称
                var box = list_Files.Children[i] as TextBox;
                var newPath = Path.Combine(newDir, box.Text);

                // 如果是目录，则移动
                if (Directory.Exists(CombineFiles[i])) {
                    Directory.Move(CombineFiles[i], newPath);
                } else {
                    File.Move(CombineFiles[i], newPath);
                }
            }

            DialogResult = true;
            Close();
        }
    }
}
